//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Collections;
using System.Web.UI.WebControls;

using CommunityServer.Components;
using CommunityServer.Controls;
using CommunityServer.Discussions.Components;
using CA = ComponentArt.Web.UI;

namespace CommunityServer.Discussions.Controls 
{

    /// <summary>
    /// Summary description for ForumListBox.
    /// </summary>
    [System.Web.UI.ValidationPropertyAttribute("SelectedValue")] 
    public class ForumListBox : TemplatedWebControl
    {
		string skinFilename	= "View-ForumListBox.ascx";
		CA.TreeView ForumTree;
		Panel ButtonPanel;

        public ForumListBox() 
        {
			this.ExternalSkinFileName = skinFilename;
        }

		protected override void AttachChildControls() 
		{
			ForumTree = FindControl("ForumTree") as CA.TreeView;
			ButtonPanel = FindControl("ButtonPanel") as Panel;
		}

		protected override void OnLoad(EventArgs e) 
		{
			if (!Page.IsPostBack) 
			{
				this.DataBind();
			}

			base.OnLoad (e);
		}

        public override void DataBind() 
        {
			base.DataBind();

			if (ForumTree.Nodes.Count > 0)
				return;

            ArrayList forumGroups;

            // Get all forum groups
            if (this.ForumMode == ControlUserMode.User)
                forumGroups = ForumGroups.GetForumGroups(false, false);
            else
                forumGroups = ForumGroups.GetForumGroups(false);

            CA.TreeViewNode node;
            foreach (Group group in forumGroups) 
            {

				// Get the sections for the group
				if(this.ForumMode == ControlUserMode.User)
					group.Sections = Forums.GetForumsByForumGroupID(group.GroupID,true,false,false,false);
				else
					group.Sections = Forums.GetForumsByForumGroupID(group.GroupID,false,true,false,false);

				if(this.ShowEmptyForumGroups || group.HasSections)
				{
					// Add the forum group
					//
					node = new CA.TreeViewNode();
					node.Text = group.Name;
					node.Value = "g-" + group.GroupID.ToString();
					node.ID = node.Value;

					RecursiveAddForum (node, group.Sections);

					ForumTree.Nodes.Add(node);
				}
            }

			if ((ButtonPanel != null) && (ShowButtons))
				ButtonPanel.Visible= true;
        }

		private bool _ShowEmptyForumGroups = false;
		public bool ShowEmptyForumGroups
		{
			get
			{
				return _ShowEmptyForumGroups;
			}
			set
			{
				_ShowEmptyForumGroups = value;
			}
		}

		private bool showButtons = false;
		public bool ShowButtons
		{
			get { return showButtons; }
			set { showButtons = value; }
		}

		public string SelectedValue
		{
			get
			{
				EnsureChildControls();
				CA.TreeViewNode node = ForumTree.SelectedNode;
				if (node != null)
					return node.Value;
				else
					return String.Empty;
			}
		}

        public int SelectedForum 
        {
            get 
            {
				EnsureChildControls();
				CA.TreeViewNode node = ForumTree.SelectedNode;
				if (node != null && node.Value.StartsWith("f"))
					return int.Parse(node.Value.Replace("f-", ""));
				else
					return 0;
            }
            set 
            {
                if (value > 0) 
                {
					// Deselect current item
					EnsureChildControls();
					if (ForumTree.SelectedNode != null)
						ForumTree.SelectedNode = null;

					ForumTree.SelectedNode = ForumTree.FindNodeById("f-" + value);
                }
            }
        }


        public int SelectedForumGroup 
        {
            get 
            {
				EnsureChildControls();
				CA.TreeViewNode node = ForumTree.SelectedNode;
				if (node != null)
				{
					if (node.Value.StartsWith("f"))
					{
						return Forums.GetForum(int.Parse(node.Value.Replace("f-", ""))).GroupID;
					}
					else if (node.Value.StartsWith("g"))
					{
						return int.Parse(node.Value.Replace("g-", ""));
					}
				}
				
                return 0;
            }
            set 
            { 
                // Deselect current item
				EnsureChildControls();
				if (ForumTree.SelectedNode != null)
					ForumTree.SelectedNode = null;

				ForumTree.SelectedNode = ForumTree.FindNodeById("g-" + value);
            }
        }

        private void RecursiveAddForum (CA.TreeViewNode parentNode, ArrayList forums) 
        {
			CA.TreeViewNode node;
            
			foreach (Forum forum in forums) 
			{
				node = new CA.TreeViewNode();
				node.Text = forum.Name;
				node.Value = "f-" + forum.SectionID.ToString();
				node.ID = node.Value;

				if (forum.Sections.Count > 0)
				{
					// Sort subforums by name
					forum.Sections.Sort(new SectionSorter(SectionSortBy.Name));

					RecursiveAddForum(node, forum.Sections);
				}
				parentNode.Nodes.Add(node);
			}
        }


        [
        System.ComponentModel.DefaultValue( ControlUserMode.User ),
        ]
        public virtual ControlUserMode ForumMode 
        {
            get 
            {
                Object state = ViewState["ForumMode"];
                if ( state != null ) 
                {
                    return (ControlUserMode)state;
                }
                return ControlUserMode.User;
            }
            set 
            {
                ViewState["ForumMode"] = value;
            }
        }

    }
}
